package com.czl.module_service.adapter

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.PurchaseBean
import com.czl.base.data.bean.tengyun.PurchaseOrderBean
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.base.util.ToastHelper.showNormalToast
import com.czl.module_service.R
import com.czl.module_service.databinding.ItemPurchaseApplyGoodsBinding
import com.czl.module_service.fragment.purchase.PurchaseApplyAddFragment
import com.czl.module_service.fragment.purchase.PurchaseApplyInfoFragment
import com.czl.module_service.view.ListSheetDialogFragment
import com.czl.module_service.viewmodel.PurchaseApplyAddViewModel

class PurchaseApplyAddAdapter(private val fragment: PurchaseApplyAddFragment, val block: () -> Unit) :
    BaseQuickAdapter<PurchaseOrderBean, BaseDataBindingHolder<ItemPurchaseApplyGoodsBinding>>(
        R.layout.item_purchase_apply_goods
    ) {

    var position : Int = 0
    var count: Int = 0

    var isDel : Boolean = false

    var model : PurchaseApplyAddViewModel? = null

    override fun convert(
        holder: BaseDataBindingHolder<ItemPurchaseApplyGoodsBinding>,
        item: PurchaseOrderBean,
    ) {
        if (item.position == 1) {
            item.isDel = false
        } else {
            item.isDel = isDel
        }
        item.position = holder.layoutPosition
        holder.dataBinding?.run {
            data = item
            adapter = this@PurchaseApplyAddAdapter
            executePendingBindings()
        }

        holder.dataBinding?.etSortNum?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
            override fun afterTextChanged(s: Editable?) {
                block()
            }
        })
        holder.dataBinding?.etSortPrice?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
            override fun afterTextChanged(s: Editable?) {
                block()
            }
        })
    }

    val onImageClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is PurchaseOrderBean) {
            position = it.position!!
            fragment.uploadFile()
        }
    })

    val getAssetTypeInfo: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if(it is PurchaseOrderBean) {
            position = it.position!!
            fragment.choiceAssetTypeEvent()
        }
    })

    val getPurchaseName: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if(it is PurchaseOrderBean) {
            position = it.position!!
            fragment.choicePurchaseNameEvent()
        }
    })

    val delSortClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is PurchaseOrderBean) {
            remove(it)
            block()
            fragment.deleteList(it)
        }
    })

}
